/*
 * @lc app=leetcode.cn id=59 lang=typescript
 *
 * [59] 螺旋矩阵 II
 */

// @lc code=start
function generateMatrix(n: number): number[][] {
    let res: any = new Array(n).fill(0).map(() => new Array(n).fill(0))
    let num = 1, l = 0, r = n - 1, t = 0, b = n - 1;
    const tar = n * n;
    while (num <= tar) {
        for (let i = l; i <= r; i++)
            res[t][i] = num++;
        t++;
        for (let i = t; i <= b; i++)
            res[i][r] = num++;
        r--;
        for (let i = r; i >= l; i--)
            res[b][i] = num++;
        b--;
        for (let i = b; i >= t; i--)
            res[i][l] = num++;
        l++;
    }
    return res
};

// @lc code=end

